package com.itheima.jobs;

import com.itheima.utils.QiniuUtils;
import com.itheima.constant.RedisConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;

import java.util.Set;

@Component
public class ClearImgJob {
    @Autowired
    private JedisPool jedisPool;

    public void ClearImg() {
        //对redis中的存储图片的两个set集合进行比较，获取垃圾图片名称
        Set<String> diffImgSet = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES,
                RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        for (String imgName : diffImgSet) {

            //删除云上的图片存储
            QiniuUtils.deleteFileFromQiniu(imgName);

            //删除redis中的图片存储
            jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,imgName);

        }
    }
}
